package com.ssh.api.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.dao.impl.DaoImpl;
import com.ssh.entity.Seller;
import com.ssh.entity.SellerAssess;
import com.ssh.entity.Task;
import com.ssh.manager.SellerAssessManager;
import com.ssh.manager.TaskManager;
@Controller
@ParentPackage("client")
@Scope("prototype")
@Results(value={@Result(name ="add",type="json",params={"root","jr","includeProperties","method,message,code,data\\[\\d+\\]\\.id","excludeProperties","data.employee.position.del,data.employee.position.createDate,data.employee.employee.*,data.employee.createDate","excludeNullProperties","true"})})

public class SellerAssessClientAction extends ActionSupport{
	private static final long serialVersionUID = 970773022382019911L;
	JsonResult jr=new JsonResult();
	private String params;
	@Resource(name="sellerAssessManagerImpl")
	private SellerAssessManager sellerAssessManager;
	@Resource(name="taskManagerImpl")
	private TaskManager taskManager;
    
	public  String add(){
		jr.setMethod("add");
		try {
		List<SellerAssess> listResult=new ArrayList<SellerAssess>();
		@SuppressWarnings("unchecked")
		Map<String, Object> json =(Map<String, Object>)JSONUtil.deserialize(params);
		Seller seller=sellerAssessManager.findSellerById(Long.parseLong(json.get("sellerId").toString()));
		Task task=taskManager.fingTaskById(Long.parseLong(json.get("taskId").toString()));
		@SuppressWarnings("unchecked")
		List<Map<String, String>> saList=(List<Map<String, String>>)json.get("data");
		for(Map<String,String> map:saList){
			SellerAssess sellerAssess=new SellerAssess();
			sellerAssess.setSeller(seller);
			sellerAssess.setTask(task);
			sellerAssess.setCreateDate(new Timestamp(System.currentTimeMillis()));
			sellerAssess.setJiqiao(Integer.parseInt(map.get("jiqiao")));
			sellerAssess.setKaoqin(Integer.parseInt(map.get("kaoqin")));
			sellerAssess.setYibiao(Integer.parseInt(map.get("yibiao")));
			sellerAssess.setZhishi(Integer.parseInt(map.get("zhishi")));
			listResult.add(sellerAssessManager.add(sellerAssess));	
		}
		jr.setCode("0");
		jr.setData(listResult);
		} catch (JSONException e) {
			 LogFactory.getDefaultLog().error(e.getLocalizedMessage(), LogName.API);
			jr.setCode("-1");
			jr.setMessage("保存失败");
		}
		return "add";
    }
	

	public JsonResult getJr() {
		return jr;
	}

	public void setJr(JsonResult jr) {
		this.jr = jr;
	}


	public void setParams(String params) {
		//params="{'sellerId':'1','taskId':'1','data':[{'yibiao':'2','kaoqin':'2','zhishi':'23','jiqiao':'2'},{'yibiao':'5','kaoqin':'3','zhishi':'22','jiqiao':'3'}]}";
		this.params = params;
	}

}
